﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class SchoolLossForm : Form
    {
        int regID, avpID, fhID, pfshID = -1, lossID, year;
        tb_schoolloss schoolLoss;

        public SchoolLossForm()
        {
            InitializeComponent();
        }

        private void SchoolLossForm_Load(object sender, EventArgs e)
        {
            cbRegion.SelectedIndexChanged -= cbRegion_SelectedIndexChanged;
            cbRegion.DataSource = UtilClass.FillCBRegions();
            cbRegion.DisplayMember = "Name";
            cbRegion.ValueMember = "ID";

            if (InformationClass.isAdmin)
            {
                cbRegion.SelectedIndex = -1;
                cbRegion.Text = string.Empty;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
            }
            else
            {
                cbRegion.Enabled = false;
                regID = InformationClass.regionID;
                cbRegion.SelectedIndexChanged += new EventHandler(cbRegion_SelectedIndexChanged);
                cbRegion.SelectedValue = regID;
                cbRegion_SelectedIndexChanged(cbRegion, null);
                cbAVP.Enabled = true;
                cbAVP.Text = string.Empty;
                //toolStrip1.Visible = false;
            }

            for (int i = 1991; i < 2100; i++)
            {
                cbYear.Items.Add(i);
            }
            cbYear.SelectedIndex = cbYear.FindStringExact(DateTime.Now.Year.ToString());

            cbLoss.SelectedIndexChanged -= cbLoss_SelectedIndexChanged;
            if (InformationClass.Lang)
                cbLoss.DataSource = DBClass.Losses
                    .Select(se => new
                    {
                        ID = se.ID,
                        Name = se.Name
                    })
                    .Distinct().ToArray();
            else
                cbLoss.DataSource = DBClass.Losses
                    .Select(se => new
                    {
                        ID = se.ID,
                        Name = se.NameUz
                    })
                    .Distinct().ToArray();

            cbLoss.DisplayMember = "Name";
            cbLoss.ValueMember = "ID";
            cbLoss.SelectedIndex = -1;
            cbLoss.Text = string.Empty;
            cbLoss.SelectedIndexChanged += new EventHandler(cbLoss_SelectedIndexChanged);

        }

        private void cbRegion_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbRegion.SelectedIndex >= 0)
            {
                cbAVP.Enabled = true;
                regID = Convert.ToInt32(cbRegion.SelectedValue);
                cbAVP.SelectedIndexChanged -= cbAVP_SelectedIndexChanged;
                cbAVP.DataSource = UtilClass.FillAVP(regID);
                cbAVP.ValueMember = "ID";
                cbAVP.DisplayMember = "Name";
                cbAVP.SelectedIndex = -1;
                cbAVP.Text = string.Empty;
                cbFH.DataSource = null;
                cbPFSH.DataSource = null;
                cbAVP.SelectedIndexChanged += new EventHandler(cbAVP_SelectedIndexChanged);
                pfshID = 0;
            }
        }

        private void cbAVP_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbAVP.SelectedIndex >= 0)
            {
                avpID = Convert.ToInt32(cbAVP.SelectedValue);
                cbFH.SelectedIndexChanged -= cbFH_SelectedIndexChanged;
                cbFH.Enabled = true;
                cbFH.DataSource = DBClass.Farms
                    .Where(wh => wh.AVPID == avpID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbFH.DisplayMember = "Name";
                cbFH.ValueMember = "ID";
                cbFH.SelectedIndex = -1;
                cbFH.Text = string.Empty;
                cbPFSH.DataSource = null;
                cbFH.SelectedIndexChanged += new EventHandler(cbFH_SelectedIndexChanged);
                pfshID = 0;
            }
        }

        private void cbFH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbFH.SelectedIndex >= 0)
            {
                fhID = Convert.ToInt32(cbFH.SelectedValue);
                cbPFSH.Enabled = true;
                cbPFSH.SelectedIndexChanged -= cbPFSH_SelectedIndexChanged;
                cbPFSH.DataSource = DBClass.Schools
                    .Where(wh => wh.FarmID == fhID)
                    .Select(se => new { ID = se.ID, Name = se.Name })
                    .OrderBy(o => o.Name)
                    .ToArray();
                cbPFSH.DisplayMember = "Name";
                cbPFSH.ValueMember = "ID";
                cbPFSH.SelectedIndex = -1;
                cbPFSH.Text = string.Empty;
                cbPFSH.SelectedIndexChanged += new EventHandler(cbPFSH_SelectedIndexChanged);
                pfshID = 0;
            }
        }

        private void cbPFSH_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbPFSH.SelectedIndex >= 0)
            {
                pfshID = Convert.ToInt32(cbPFSH.SelectedValue);
                if (pfshID > 0 && lossID > 0)
                    ShowSchoolLoss(pfshID, lossID, year);
            }
            else
            {
                pfshID = 0;
            }
        }

        private void cbYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbYear.SelectedIndex >= 0)
            {
                year = Convert.ToInt32(cbYear.Items[cbYear.SelectedIndex].ToString());
                if (pfshID > 0 && lossID > 0)
                    ShowSchoolLoss(pfshID, lossID, year);
            }
            else
                year = DateTime.Now.Year;
        }

        private void cbLoss_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbLoss.SelectedIndex >= 0)
            {
                lossID = Convert.ToInt32(cbLoss.SelectedValue);
                if (pfshID > 0 && lossID > 0)
                    ShowSchoolLoss(pfshID, lossID, year);
            }
            else
                lossID = 0;
        }

        private void ShowSchoolLoss(int pfshID, int lossID, int year)
        {
            schoolLoss = DBClass.SchoolLosses
                .Where(wh => wh.LossID == lossID && wh.SchoolID == pfshID && wh.Year == year)
                .SingleOrDefault();
            if (schoolLoss != null)
            {
                tbAmount.Text = schoolLoss.Amount.ToString();
            }
            else
                tbAmount.Text = string.Empty;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (pfshID > 0 && lossID > 0)
            {
                decimal amount;
                if (!tbAmount.Text.GetDecimalOrDefault(out amount))
                    if (MessageBox.Show("Не верно заполненно значение. Продолжить?", "Внимание", MessageBoxButtons.YesNo) == DialogResult.No)
                        return;

                if (schoolLoss != null)
                {
                    schoolLoss.Amount = amount;
                }
                else
                {
                    schoolLoss = new tb_schoolloss
                    {
                        Year = year,
                        SchoolID = pfshID,
                        LossID = lossID,
                        Amount = amount
                    };
                    DBClass.SchoolLosses.InsertOnSubmit(schoolLoss);
                }
                try
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                catch
                {
                    MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
                }
            }
        }
    }
}
